home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1993 July / InfoMagic USENET CD-ROM July 1993.ISO / sources / unix / volume4 / portar < prev    next >
Encoding:
Internet Message Format  |  1986-11-30  |  6.0 KB

  1. From: talcott!cbpavo.cbosgd.ATT:ulysses!mark (Mark Horton)
  2. Subject: portable ar: suggested replacement for shar
  3. Newsgroups: mod.sources
  4. Approved: jpn@panda.UUCP
  5.  
  6. Mod.sources:  Volume 4, Issue 81
  7. Submitted by: talcott!cbpavo.cbosgd.ATT:ulysses!mark (Mark Horton)
  8.  
  9. [
  10.   This is one of two submissions of programs that provide alternatives to
  11.   "shar" as a source archive format.  This does not imply my support of
  12.   one format over another.  Oh, I am distributing these in "shar" format
  13.   to avoid the chicken-or-egg problem.
  14.     - John Nelson - moderator, mod.sources
  15. ]
  16.  
  17. #!/bin/sh
  18. # This is a shell archive, meaning:
  19. # 1. Remove everything above the #!/bin/sh line.
  20. # 2. Save the resulting text in a file.
  21. # 3. Execute the file with /bin/sh (not csh) to create the files:
  22. #    Makefile
  23. #    README
  24. #    arwrite.c
  25. #    main.c
  26. #    sample
  27. #    unpar.c
  28. # This archive created: Mon Apr 21 09:41:06 1986
  29. export PATH; PATH=/bin:$PATH
  30. if test -f 'Makefile'
  31. then
  32.     echo shar: over-writing existing file "'Makefile'"
  33. fi
  34. cat << \SHAR_EOF > 'Makefile'
  35.  
  36. par:    arwrite.o main.o
  37.     cc -o par main.o arwrite.o
  38. unpar:    unpar.o
  39.     cc -o unpar unpar.o
  40. clean:
  41.     rm par unpar *.o
  42. SHAR_EOF
  43. if test -f 'README'
  44. then
  45.     echo shar: over-writing existing file "'README'"
  46. fi
  47. cat << \SHAR_EOF > 'README'
  48. These programs pack and unpack files in the portable archive format.
  49. This format is compatible with the ar command on 4BSD and System V
  50. release 2.  These programs are available for use by anyone without
  51. fee or license.
  52.  
  53. The par program packs into portable archive format.  The files to
  54. write are named on the command line, the resulting archive is
  55. written on stdout:
  56.     par file1 file2 ... filen > files.a
  57.  
  58. The unpar program unpacks from stdin and writes the named files
  59. out directly:
  60.     unpar < files.a
  61. SHAR_EOF
  62. if test -f 'arwrite.c'
  63. then
  64.     echo shar: over-writing existing file "'arwrite.c'"
  65. fi
  66. cat << \SHAR_EOF > 'arwrite.c'
  67. /*
  68.  *        COPYRIGHT (c) HEWLETT-PACKARD COMPANY, 1984
  69.  *
  70.  *    Permission is granted for unlimited modification, use, and
  71.  *    distribution except that this software may not be sold for
  72.  *    profit.  No warranty is implied or expressed.
  73.  *
  74.  *Author:
  75.  *    Paul Bame, HEWLETT-PACKARD LOGIC SYSTEMS DIVISION - Colorado Springs
  76.  *    { ihnp4!hpfcla | hplabs | harpo!hp-pcd }!hp-lsd!paul
  77.  */
  78. #include <stdio.h>
  79. #include <sys/types.h>
  80. #include <sys/stat.h>
  81.  
  82. #define ARFMAG "`\n"
  83. #define ARMAG "!<arch>\n"
  84.  
  85. int
  86. arwrite(filename,stream)
  87. char *filename;
  88. FILE *stream;
  89. /*
  90.  *    Write the file denoted by 'filename' to 'stream' in portable ar format.
  91.  *    Any stream positioning is assumed to already be done.
  92.  *
  93.  *    arwrite() normally returns 0 but returns -1 in case of errors.  errno
  94.  *    may be inspected to determine the cause of the error.
  95.  *
  96.  *    If necessary, the stream is padded with \n's to match the even boundary
  97.  *    condition.
  98.  */
  99. {
  100.     struct stat statbuf;
  101.     FILE *instream;
  102.     long i;
  103.  
  104.     /* stat() the file to get the stuff for the header */
  105.     if( stat(filename,&statbuf) < 0 )
  106.     {
  107.         /* error! */
  108.         return(-1);
  109.     }
  110.  
  111.     /* Open file for reading */
  112.     if( (instream = fopen(filename,"r")) == NULL )
  113.     {
  114.         /* error! */
  115.         return(-1);
  116.     }
  117.  
  118.     /* Now write the header */
  119.     /* This information gleaned from ar(4) in V.2 */
  120.     fprintf(stream,
  121.         "%-16s%-12ld%-6d%-6d%-8o%-10ld%2s",
  122.         filename,
  123.         statbuf.st_mtime,
  124.         statbuf.st_uid,
  125.         statbuf.st_gid,
  126.         statbuf.st_mode,
  127.         statbuf.st_size,
  128.         ARFMAG
  129.         );
  130.  
  131.     /* And copy the file */
  132.     /*   Note that there is no error recovery here! */
  133.     for( i = 0 ; i < statbuf.st_size ; i++ )
  134.     {
  135.         fputc(fgetc(instream),stream);
  136.     }
  137.  
  138.     /* and close */
  139.     close(instream);
  140.  
  141.     /* and pad output stream */
  142.     if( (ftell(stream) & 0x1) != 0 )
  143.     {
  144.         /* if offset is odd, pad with a nuline */
  145.         fputc('\n',stream);
  146.     }
  147.     return(0);
  148. }
  149.  
  150. arhdwrite(stream)
  151. FILE *stream;
  152. /*
  153.  *    Write the archive header onto 'stream'
  154.  */
  155. {
  156.     fprintf(stream,"%s",ARMAG);
  157. }
  158. SHAR_EOF
  159. if test -f 'main.c'
  160. then
  161.     echo shar: over-writing existing file "'main.c'"
  162. fi
  163. cat << \SHAR_EOF > 'main.c'
  164. /*
  165.  *        COPYRIGHT (c) HEWLETT-PACKARD COMPANY, 1984
  166.  *
  167.  *    Permission is granted for unlimited modification, use, and
  168.  *    distribution except that this software may not be sold for
  169.  *    profit.  No warranty is implied or expressed.
  170.  *
  171.  *Author:
  172.  *    Paul Bame, HEWLETT-PACKARD LOGIC SYSTEMS DIVISION - Colorado Springs
  173.  *    { ihnp4!hpfcla | hplabs | harpo!hp-pcd }!hp-lsd!paul
  174.  */
  175. #include <stdio.h>
  176.  
  177. extern    int    arwrite();
  178. extern        arhdwrite();
  179.  
  180. main(argc,argv)
  181. int argc;
  182. char *argv[];
  183. /*
  184.  *    Write the named files (in argv) to stdout in portable ar format.
  185.  */
  186. {
  187.     int i;
  188.  
  189.     if( argc < 2 )
  190.     {
  191.         fprintf(stderr,"Usage: %s file1 [file...]\n",argv[0]);
  192.         exit(1);
  193.     }
  194.  
  195.     /* Write the header */
  196.     arhdwrite(stdout);
  197.  
  198.     /* and the files */
  199.     for( i = 1 ; i < argc ; i++ )
  200.     {
  201.         if( arwrite(argv[i],stdout) < 0 )
  202.         {
  203.             perror(argv[i]);
  204.             exit(1);
  205.         }
  206.     }
  207.     exit(0);
  208. }
  209. SHAR_EOF
  210. if test -f 'sample'
  211. then
  212.     echo shar: over-writing existing file "'sample'"
  213. fi
  214. cat << \SHAR_EOF > 'sample'
  215. !<arch>
  216. foo             458764309   12    0     100666  29        `
  217. Sun Jul 15 14:31:49 EDT 1984
  218.  
  219. bar             458764313   12    0     100666  189       `
  220. sarge    ttya    Jul 13 10:02
  221. jgs      ttys1   Jul 15 10:45
  222. mark     ttyp0   Jul 15 14:07    (cbosgd)
  223. mark     ttyp1   Jul 13 09:36
  224. mark     ttyp2   Jul 13 09:36
  225. mark     ttyp3   Jul 13 16:57
  226.  
  227.  
  228. SHAR_EOF
  229. if test -f 'unpar.c'
  230. then
  231.     echo shar: over-writing existing file "'unpar.c'"
  232. fi
  233. cat << \SHAR_EOF > 'unpar.c'
  234. /* unpack portable archives - written by Bill Welch */
  235. #include <stdio.h>
  236. #include <sys/types.h>
  237.  
  238. char buf[512];
  239. main()
  240. {
  241.     char name[80];
  242.     struct utimbuf {
  243.         time_t actime;
  244.         time_t modtime;
  245.     } times;
  246.     long mtime;
  247.     int uid, gid, mode;
  248.     long len, i;
  249.     FILE *fp;
  250.  
  251.     while(gets(buf) != NULL){
  252.         if (buf[strlen(buf) - 1] == '`') {
  253.             printf("%s\n", buf);
  254.             sscanf(buf, "%s %ld %d %d %o %ld", name,&mtime,&uid,&gid,
  255.                     &mode, &len);
  256.             printf("%s %ld\n", name, len);
  257.             fp = fopen(name, "w");
  258.             for (i=0; i<len; i++) putc(getchar(), fp);
  259.             fclose(fp);
  260.             times.actime = times.modtime = mtime;
  261.             /* If you don't have utime, just remove next 4 lines */
  262.             if( utime(name,×) < 0 )
  263.             {
  264.                 perror("Can't modify date");
  265.             }
  266.             if( chmod(name,mode) < 0 )
  267.             {
  268.                 perror("Can't modify mode");
  269.             }
  270.         }
  271.     }
  272. }
  273. SHAR_EOF
  274. #    End of shell archive
  275. exit 0
  276.  
  277.  
  278.  
  279.  
  280.